import requests

def get_address_info(address,amap_key):
    url = f"https://restapi.amap.com/v3/geocode/geo?key={amap_key}&address={address}"
    payload = {}
    headers = {}
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.json()
def get_address(location,amap_key):
    url = f"https://restapi.amap.com/v3/geocode/regeo?key={amap_key}&location={location}"
    payload = {}
    headers = {}
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.json()
def loacation2address(location,gaode_key):
    result={}
    jsonData=get_address(location,gaode_key)
    # print('jsonData',jsonData)
    if jsonData['infocode']=='10000':
        try:
            if len(jsonData['regeocode']['addressComponent']['city'])>0:
                result['city']=jsonData['regeocode']['addressComponent']['city']
                result['province']=jsonData['regeocode']['addressComponent']['province']
                result['district']=jsonData['regeocode']['addressComponent']['district']
                result['township']=jsonData['regeocode']['addressComponent']['township']
        except:
            pass
    return result
if __name__=='__main__':
    address='广州市北京路'
    location='39.984154,116.307490'
    from keys import gaode_key #高德地图api keys
    location='113.271917,23.096571972222222,'
    # print(get_address_info(address,amap_key))
    result=loacation2address(location,gaode_key)